package com.mini.spring.beanfactory.support;

import com.mini.spring.beanfactory.factory.BeanDefinition;
import com.mini.spring.BeansException;
import com.mini.spring.beanfactory.BeanFactory;

/**
 * @author: 屠一乐
 * @date: 2023/8/20 17:01
 * @description:
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) throws BeansException {
        //先从单例注册表中获取
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }
        //获取不到的话，就通过获取BeanDefinition的方式获取BeanDefinition（注册的时候存入的BeanDefinition）
        BeanDefinition beanDefinition = getBeanDefinition(name);
        //获取到BeanDefinition后，再通过BeanDefinition去创建Bean，然后存入单例注册表中
        return createBean(name, beanDefinition);
    }

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;
}
